package com.xyht.sca_s.student_manage_system.modules.scheduleManagement.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.scheduleManagement.entity.req.SmsScheduleDeleteReq;
import com.xyht.sca_s.student_manage_system.modules.scheduleManagement.entity.req.SmsScheduleReadReq;
import com.xyht.sca_s.student_manage_system.modules.scheduleManagement.entity.req.SmsScheduleReq;
import com.xyht.sca_s.student_manage_system.modules.scheduleManagement.entity.req.SmsScheduleUpdateReq;
import com.xyht.sca_s.student_manage_system.modules.scheduleManagement.service.SmsScheduleService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Future;

/**
 * <p>
 * 日程表 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2023-08-14
 */
@RestController
@RequestMapping("/schedule")
@SecretBody
public class SmsScheduleController {
    @Autowired
    private SmsScheduleService smsScheduleService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @ApiModelProperty(value = "添加日程")
    @PostMapping("/add")
    public ResponseResult addSchedule(@RequestBody @Validated SmsScheduleReq smsScheduleReq, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        ResponseResult responseResult;
        try {
            Future<ResponseResult> future = smsScheduleService.addSchedule(smsScheduleReq, user_id);
            while (true) {
                if (future.isDone()) {
                    break;
                }
            }
            responseResult = future.get();
        } catch (Exception e) {
            e.printStackTrace();
            responseResult = CommonResult.failed(e.getMessage());
        }
        return responseResult;
    }

    @ApiModelProperty(value = "查询日程列表")
    @GetMapping("/list")
    public ResponseResult getScheduleListByTimeRange(@RequestParam("bg_date") Date bg_date,
                                                     @RequestParam("end_date") Date end_date,
                                                     @RequestParam(value = "subscribe_user",required = false) List<String>  subscribe_user,
                                                     HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsScheduleService.getScheduleListByTimeRange(bg_date, end_date, subscribe_user, user_id);
    }

    @ApiModelProperty(value = "查询日程详情")
    @GetMapping("/detail")
    public ResponseResult getScheduleDetail(@RequestParam("schedule_id") String schedule_id) {
        return smsScheduleService.getScheduleDetail(schedule_id);
    }

    @ApiModelProperty(value = "修改日程")
    @PostMapping("/update")
    public ResponseResult updateSchedule(@RequestBody @Validated SmsScheduleUpdateReq smsScheduleReq, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        ResponseResult responseResult;
        try {
            Future<ResponseResult> future = smsScheduleService.updateSchedule(smsScheduleReq, user_id);
            while (true) {
                if (future.isDone()) {
                    break;
                }
            }
            responseResult = future.get();
        } catch (Exception e) {
            e.printStackTrace();
            responseResult = CommonResult.failed(e.getMessage());
        }
        return responseResult;
    }

    @ApiModelProperty(value = "删除日程")
    @PostMapping("/delete")
    public ResponseResult deleteSchedule(@RequestBody @Validated SmsScheduleDeleteReq smsScheduleReq, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        ResponseResult responseResult;
        try {
            Future<ResponseResult> future = smsScheduleService.deleteSchedule(smsScheduleReq, user_id);
            while (true) {
                if (future.isDone()) {
                    break;
                }
            }
            responseResult = future.get();
        } catch (Exception e) {
            e.printStackTrace();
            responseResult = CommonResult.failed(e.getMessage());
        }
        return responseResult;
    }
    @ApiModelProperty(value = "日程已读")
    @PostMapping("/read")
    public ResponseResult deleteSchedule(@RequestBody SmsScheduleReadReq smsScheduleReq, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsScheduleService.scheduleRead(smsScheduleReq,user_id);
    }


}

